class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        int pre = 0;
        int suf = 0;
        vector<int> v;
        std::sort(nums1.begin(), nums1.end());
        std::sort(nums2.begin(), nums2.end());
        while (pre < nums1.size() && suf < nums2.size())
        {
            if (nums1[pre] < nums2[suf])
            {
                pre++;
                continue;
            }
            if (nums2[suf] < nums1[pre])
            {
                suf++;
                continue;
            }
            if (nums1[pre] == nums2[suf])
            {
                v.push_back(nums1[pre]);
                pre++;
                suf++;
                while (pre < nums1.size() && nums1[pre] == nums1[pre - 1])
                {
                    pre++;
                }
                while (suf < nums2.size() && nums2[suf] == nums2[suf - 1])
                {
                    suf++;
                }
            }

        }
        return v;
    }
};